﻿#ifndef PLFinderH
#define PLFinderH

#include <functional>
#include "Map.h"
#include "LineFinder.h"
#include "ThreadPool.h"

class PLFinderBase : public Map
{
protected:
    using PointPtr = Cell* (Map::*)(int, int);   //	typedef Cell* (Map::*PointPtr)(int);   // 类函数指针
    using Callback = std::function<bool(int, bool, int, unsigned int)>;     // int (*)(int, bool, int, unsigned int);
    void recExhaNum(unsigned int n);
    bool procCol;
    Callback callback;
private:
    void clear();
    int exec2();
    virtual void execALoop(int n, PointPtr ptr, int len, Flags flags[]) = 0;
public:
    int threadNum = 1;
    int findSteps, timeUsec;
    unsigned int exhaNum1st, exhaNum2nd, exhaSum;
    bool needCallback;

    PLFinderBase() : needCallback(false) {}
    explicit PLFinderBase(Callback callbackProg) : callback(callbackProg), needCallback(true) {}
	virtual int exec(bool needClear = true, int thrNum = 0);
};

// 多线程
class PLFinderMT : public PLFinderBase
{
private:
	std::unique_ptr<ThreadPool> pthrPool;
    void execALoop(int n, PointPtr ptr, int len, Flags flags[]) override;
public:
    using PLFinderBase::PLFinderBase;
	int exec(bool needClear = true, int thrNum = 0) override;
};

// 单线程
class PLFinderST : public PLFinderBase, private LineFinder
{
private:
    void execALoop(int n, PointPtr ptr, int len, Flags flags[]) override;
public:
    using PLFinderBase::PLFinderBase;
};

#endif
